<?php
namespace Products\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;

class CategoriesTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function addCategory($categoryname)
    {	
		if(isset($_SESSION['user']['user_id']) && $_SESSION['user']['user_id']!=""){
			$userid=$_SESSION['user']['user_id'];
		}else{
			$userid='';
		}
		$data = array(
			'category_name' 	 => ucfirst($categoryname['cat_name']), 	
			'add_date_time' 	 => date('Y-m-d H:i:s'),	
			'c_status'           => 1,  		
			'user_id'           => $userid,  		
		);	
		if(isset($categoryname['category_id']) && $categoryname['category_id']!=""){
			$this->tableGateway->update($data, array('category_id' => $categoryname['category_id']));				
		}else{
			$this->tableGateway->insert($data);
		}			
		return $this->tableGateway->lastInsertValue;
    }
	public function getallCategories(){
		$select = $this->tableGateway->getSql()->select();
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;	        
	} 
    public function getallCategory($categoryId){
	    $select = $this->tableGateway->getSql()->select();
		$select->where('category_id="'.$categoryId.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet->current();	        
	}
	public function deleteCategory($category_id){
		$deleteid=$this->tableGateway->delete(array('(category_id IN ('.$category_id.'))'));
		return $deleteid;
	} 	
	public function checkCategoryName($categoryName){
		$cat_name=ucfirst($categoryName);
		$select = $this->tableGateway->getSql()->select();
		$select->where('category_name="'.$cat_name.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet->count();
	}
}